package com.wmx.thread.dayTwo;

import java.util.ArrayList;
import java.util.List;

public class MainThread {

    boolean  flg = true;

    public void sub(int j){
        System.out.println("打印子循环"+j+"次");
    }

    public void main(int j){
        System.out.println("打印主循环"+j+"次");
    }





  //  题目一：子线程循环2次，主线程循环2次，然后子线程循环2次，主线程循环2次，这样循环10次；

    public static void main(String[] args) {


        MainThread mainThread = new MainThread();
       Runnable runnable = new Runnable() {
           @Override
           public void run() {
               synchronized (mainThread){
                   for (int i = 0 ;i<=10;i++){
                       if(mainThread.flg){
                               mainThread.flg = false;
                               for (int j=1;j<=2;j++){
                                   mainThread.sub(j);
                               }

                               mainThread.notify();

                       }else {
                           try {
                               mainThread.wait();
                           }catch (Exception e){
                               e.printStackTrace();
                           }

                       }
                   }
               }
           }
       };

       new Thread(runnable).start();

       synchronized (mainThread){
           for (int i = 0 ;i<=10;i++) {
               if (!mainThread.flg) {
                   mainThread.flg = true;
                   for (int j=1;j<=2;j++){
                       mainThread.main(j);
                   }

                   mainThread.notify();

               } else {
                   try {
                       mainThread.wait();
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           }
       }




    }
}
